<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace cmf\paginator;

use think\Paginator;

class Layui extends Paginator
{
    /**
     * 上一页按钮
     * @param string $text
     * @return string
     */
    protected function getPreviousButton($text = "")
    {

        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url(
            $this->currentPage() - 1
        );

        return $this->getPageLinkWrapper($url, $text,1);
    }

    /**
     * 下一页按钮
     * @param string $text
     * @return string
     */
    protected function getNextButton($text = '')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->currentPage() + 1);

        return $this->getPageLinkWrapper($url, $text,1);
    }

    /**
     * 上一页按钮
     * @param string $text
     * @return string
     */
    protected function getSimplePreviousButton($text = "")
    {

        if ($this->currentPage() <= 1) {
            //return '<li class="disabled previous"><span>' . $text . '</span></li>';
            return '<a href="javascript:;" class="layui-laypage-prev layui-disabled" data-page="0"><i class="layui-icon">' . $text . '</i></a>';
        }

        $url = $this->url(
            $this->currentPage() - 1
        );

        //return '<li class="previous"><a href="' . htmlentities($url) . '">' . $text . '</a></li>';
        return '<a href="' . htmlentities($url) . '" class="layui-laypage-prev" data-page="'.($this->currentPage() - 1).'"><i class="layui-icon">' . $text . '</i></a>';
    }

    /**
     * 下一页按钮
     * @param string $text
     * @return string
     */
    protected function getSimpleNextButton($text = '')
    {
        if (!$this->hasMore) {
            //return '<li class="disabled next"><span>' . $text . '</span></li>';
            return '<a href="javascript:;" class="layui-laypage-next layui-disabled" ><i class="layui-icon">' . $text . '</i></a>';
        }

        $url = $this->url($this->currentPage() + 1);

        //return '<li class="next"><a href="' . htmlentities($url) . '">' . $text . '</a></li>';
        return '<a href="' . htmlentities($url) . '" class="layui-laypage-next" data-page="'.($this->currentPage()+1).'"><i class="layui-icon">' . $text . '</i></a>';
    }

    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        //dump($this->hasMore);dump($this->lastPage);dump($this->simple);dump($this->currentPage);die(); bool(true) int(9) bool(false) int(1)
        if ($this->simple)
            return '';

        $block = [
            'first'  => null,
            'slider' => null,
            'last'   => null
        ];

        $side   = 3;
        $window = $side * 2;

        // if ($this->lastPage < $window + 6) {
        //     $block['first'] = $this->getUrlRange(1, $this->lastPage);
        // } elseif ($this->currentPage <= $window) {
        //     $block['first'] = $this->getUrlRange(1, $window + 2);
        //     $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
        // } elseif ($this->currentPage > ($this->lastPage - $window)) {
        //     $block['first'] = $this->getUrlRange(1, 2);
        //     $block['last']  = $this->getUrlRange($this->lastPage - ($window + 0), $this->lastPage);
        // } else {
        //     $block['first']  = $this->getUrlRange(1, 2);
        //     $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
        //     $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
        // }

        if ($this->lastPage < 8) {
            $block['first'] = $this->getUrlRange(1, $this->lastPage);
        } else {
            if ($this->currentPage <= $window-1) {
                $block['first'] = $this->getUrlRange(1, $window);
                $block['last'] = $this->getUrlRange($this->lastPage, $this->lastPage);
            } else {
                if ( $this->currentPage >= ($this->lastPage - $side  ) ) {
                    $block['first'] = $this->getUrlRange(1,1);
                    $block['last'] = $this->getUrlRange($this->lastPage - $window+1, $this->lastPage);
                } else {
                    $block['first'] = $this->getUrlRange(1,1);
                    $block['slider'] = $this->getUrlRange($this->currentPage - $side+1, $this->currentPage + $side-1);
                    $block['last'] = $this->getUrlRange($this->lastPage, $this->lastPage);
                }
                
            }
        }  

        $html = '';

        if (is_array($block['first'])) {
            $html .= $this->getUrlLinks($block['first']);
        }

        if (is_array($block['slider'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['slider']);
        }

        if (is_array($block['last'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['last']);
        }

        return $html;
    }

    /**
     * 渲染分页html
     * @return mixed
     */
    public function render($arr=[])
    {
        if ($this->hasPages()) {
            $request = request();

            if ($this->simple || $request->isMobile()) {
                return sprintf(
                    '%s %s',
                    $this->getSimplePreviousButton(),
                    $this->getSimpleNextButton()
                );
            } else {
                if (empty($arr)){
                    $arr = [
                        0=>10,
                        1=>15,
                        2=>20
                    ];
                }
                return sprintf(
                    '%s %s %s %s',
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton(),
                    $this->getTurnButton($arr)
                );
            }
        }
    }


    /**
     * 生成跳转按钮及展示条数选择框
     *
     * @param  arr $arr
     * @return string
     */
    protected function getTurnButton($arr=[])
    {
        
        $url = $this->options['path']; 
        $wz = strpos($this->options['path'], '&');
        if ($wz) {
            $url = substr ($this->options['path'], 0, $wz); 
        }
        if ( count($this->options['query']) > 0 ) {
            foreach ($this->options['query'] as $k => $v) {
                $url .= '&'.$k.'='.$v;
            }
        }

        if ( empty($arr) ) {
            $str1 = '';$str2 = '';$str3 = '';
            if ( $this->listRows == 10 ) {
                $str1 = 'selected=""';
            } else if ( $this->listRows == 15 ) {
                $str2 = 'selected=""';
            } else if ( $this->listRows == 20 ) {
                $str3 = 'selected=""';
            } else {
                $str1 = 'selected=""';
            }
            return '<span class="layui-laypage-skip">到第<input type="text" min="1" value="1" class="layui-input">页
                            <button type="button" class="layui-laypage-btn">确定</button>
                        </span>
                        <span class="layui-laypage-count">共 '.$this->total().' 条</span>
                        <span class="layui-laypage-limits">
                            <select lay-ignore="">
                                <option value="'.$url.'&pagesize=10'.'" '.$str1.' >10 条/页</option>
                                <option value="'.$url.'&pagesize=15'.'" '.$str2.' >15 条/页</option>
                                <option value="'.$url.'&pagesize=20'.'" '.$str3.' >20 条/页</option>
                            </select>
                        </span>';
        } else {
            $html = '<span class="layui-laypage-skip">到第<input type="text" id="tunum" min="1" value="'.$this->currentPage.'" class="layui-input">页
                            <button type="button" class="layui-laypage-btn" onclick="turnpage()">确定</button>
                        </span>
                        <span class="layui-laypage-count">共 '.$this->total().' 条</span><span class="layui-laypage-limits">
                            <select lay-ignore="" onchange="window.location=this.value;" >';
            foreach ($arr as $k => $v) {
                $sel = '';
                if ($this->listRows == $v) {
                    $sel = 'selected=""';
                }
                $html .= '<option value="'.$url.'&pagesize='.$v.'" '.$sel.' >'.$v.' 条/页</option>';
            }
            $html .= '</select></span>';
            $html .= '<script type="text/javascript">function turnpage(){ if( $("#tunum").val() > 0 ){window.location="'.$url.'&pagesize='.$this->listRows.'&page="+$("#tunum").val();}else{window.location.reload();} }</script>'; 

            return $html;           
        }
        
    }


    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page,$type = 0)
    {
        //return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
        if ($type == 0) {
            return '<a href="' . htmlentities($url) . '" data-page="' . $page . '">' . $page . '</a>';
        } else {
            return '<a href="' . htmlentities($url) . '" class="layui-laypage-prev" ><i class="layui-icon">' . $page . '</i></a>';
        }
        
    }

    /**
     * 生成一个禁用的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper($text,$type = 0)
    {
        //return '<li class="disabled"><span>' . $text . '</span></li>';

        return '<a href="javascript:;" class="layui-laypage-prev layui-disabled" data-page="0"><i class="layui-icon">'.$text.'</i></a>';
    }

    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        //return '<li class="active"><span>' . $text . '</span></li>';
        return '<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>' . $text . '</em></span>';
    }

    /**
     * 生成省略号按钮
     *
     * @return string
     */
    protected function getDots()
    {
        //return $this->getDisabledTextWrapper('...');
        return '<span class="layui-laypage-spr">…</span>';
    }

    /**
     * 批量生成页码按钮.
     *
     * @param  array $urls
     * @return string
     */
    protected function getUrlLinks(array $urls)
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }

        return $html;
    }

    /**
     * 生成普通页码按钮
     *
     * @param  string $url
     * @param  int $page
     * @return string
     */
    protected function getPageLinkWrapper($url, $page,$type = 0)
    {
        if ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }

        return $this->getAvailablePageWrapper($url, $page,$type);
    }


}
